<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="jquery-3.6.0.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        div {
            width: 1200px;
        }
        
        .one {
            height: 400px;
            background-color: burlywood;
            border: 1px solid burlywood;
        }
        
        .two {
            height: 600px;
            background-color: pink;
            border: 1px solid pink;
        }
        
        .nav {
            position: relative;
        }
        
        .three {
            height: 300px;
            background-color: lightblue;
            border: 1px solid lightblue;
        }
        
        .four {
            height: 700px;
            background-color: yellow;
            border: 1px solid yellow;
        }
        
        .sidebar {
            width: 50px;
            height: 200px;
            background-color: violet;
            position: absolute;
            top: 50px;
            right: 2px;
        }
        
        .tops {
            width: 40px;
            height: 40px;
            background-color: #777;
            position: fixed;
            bottom: 20px;
            right: 5px;
        }
    </style>
</head>

<body>
    <div id="box">
        <div class="one"></div>

        <div class="nav">
            <div class="two"></div>
            <div class="sidebar">one</div>
        </div>
        <div class="three"></div>
        <div class="four"></div>
    </div>
    <div class="tops">

    </div>
    <script>
        $(function() {
            var l = $(".sidebar").offset().left;
            $(window).scroll(function() {
                if ($(document).scrollTop() >= $(".nav").offset().top) {
                    $(".sidebar").css("position", "fixed");
                    $(".sidebar").offset({
                        left: l
                    })
                } else {
                    $(".sidebar").css("position", "absolute");
                }
            })
        })
    </script>


</body>


</html>